package com.myharos.fingerguessing.utils;

import com.myharos.fingerguessing.MyApplication;
import com.myharos.fingerguessing.ResourceTable;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

public class ToastUtils {

    private static ToastDialog mToastDialog;

    public static final void showMsg(String msg) {
        showMsg(msg, 2000);
    }

    public static final void showMsg(String msg, int duration) {
        Context context = MyApplication.getInstance();
        if (mToastDialog == null) {
            mToastDialog = new ToastDialog(context);
            //设置位置
            //如果不写，默认居中
            mToastDialog.setAlignment(LayoutAlignment.CENTER);
//            //如果不写，默认包裹内容
//            mToastDialog.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT,
//                    DirectionalLayout.LayoutConfig.MATCH_CONTENT);
            //设置自动关闭
            //如果不写，就是自动关闭
            mToastDialog.setAutoClosable(true);
            //设置持续时间
            //如果不写，默认2秒
            mToastDialog.setDuration(duration);

            //让弹框出现
            DirectionalLayout dlLayout = (DirectionalLayout) LayoutScatter.getInstance(context).parse(ResourceTable.Layout_view_mytoast, null, false);
            if (dlLayout != null) {
                mToastDialog.setContentCustomComponent(dlLayout);
                Text msgText = (Text) dlLayout.findComponentById(ResourceTable.Id_msg);
                if(null != msgText){
                    msgText.setText(msg);
                }
            }else{
                mToastDialog.setText(msg);
            }
        }
        mToastDialog.show();
    }

    /**
     * 显示简单文本信息
     *
     * @param msg
     */
    public static final void showMsg(Context context,String msg, int duration) {

        if (mToastDialog == null) {
            mToastDialog = new ToastDialog(context);
            //设置位置
            //如果不写，默认居中
            mToastDialog.setAlignment(LayoutAlignment.CENTER);
//            //如果不写，默认包裹内容
//            mToastDialog.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT,
//                    DirectionalLayout.LayoutConfig.MATCH_CONTENT);
            //设置自动关闭
            //如果不写，就是自动关闭
            mToastDialog.setAutoClosable(true);
            //设置持续时间
            //如果不写，默认2秒
            mToastDialog.setDuration(duration);

            //让弹框出现
            DirectionalLayout dlLayout = (DirectionalLayout) LayoutScatter.getInstance(context).parse(ResourceTable.Layout_view_mytoast, null, false);
            if (dlLayout != null) {
                mToastDialog.setContentCustomComponent(dlLayout);
                Text msgText = (Text) dlLayout.findComponentById(ResourceTable.Id_msg);
                if(null != msgText){
                    msgText.setText(msg);
                }
            }else{
                mToastDialog.setText(msg);
            }
        }
        mToastDialog.show();
    }

    public static final void showMsg(Context context, String msg) {
        showMsg(context, msg, 2000);
    }

    public static final void showMsgLong(Context context,String msg) {
        showMsg(context, msg, 4000);
    }

    /**
     * 取消显示
     */
    public static final void cancelMsg() {
        if (mToastDialog != null) {
            mToastDialog.cancel();
        }
    }


}
